package csu.web.credit_bank.controller;

import csu.web.credit_bank.pojo.dto.PostCreateDto;
import csu.web.credit_bank.pojo.Posts;
import csu.web.credit_bank.service.PostsService;
import csu.web.credit_bank.utils.Result;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/posts")
@CrossOrigin
public class GetPostController {
    @Autowired
    private PostsService postsService;
    @Autowired
    private UUIDManager uuidManager;
    @PostMapping("")
    public ResponseEntity<Map<String, Object>> createPost(@RequestBody PostCreateDto postDto){
        String uid=postsService.createPost(postDto);
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 200);
        responseBody.put("message", "帖子发布成功");
        responseBody.put("data", uid);
        return ResponseEntity.ok(responseBody);
    }
    @GetMapping("/{id}")
    public ResponseEntity<List<Posts>> getPostsList(@PathVariable String id){
        List<Posts> postsList = postsService.getPostsList(id);
        return ResponseEntity.ok(postsList);
    }
    @PutMapping("{postId}/{userId}")
    public ResponseEntity<Map<String, Object>> updatePostLikes(@PathVariable String postId,@PathVariable String userId){
        postsService.updatePostLikes(postId,userId);
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("code", 200);
        responseBody.put("message", "点赞状态修改成功");
        responseBody.put("data", postId);
        return ResponseEntity.ok(responseBody);
    }
    @GetMapping("/todayNum/{userId}")
    public Result getTodayNum(@PathVariable String userId){
        int todayNum=postsService.getTodayNum(userId);
        return Result.success(todayNum);
    }
}
